<?php
namespace app\common\controller;
use app\common\model\User;
use app\common\model\UserAuthRule;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;
use think\facade\Cookie;
use think\Model;

class ApiBase extends Base
{
    protected $noLogin = []; // 不用权限认证和登录的方法
    protected $noAuth = []; // 不用权限认证要登录的方法

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        !$this->checkLogin() && $this->result('',0,'您没有登录!');
        //!$this->checkAuth() && $this->result('','-2','您没有操作权限');
        //!$this->checkTime() && $this->result('',-2,'您的账户已过期!');
    }

    // 检查登录
    public function checkLogin()
    {
        if (!is_api_login() &&
            !in_array($this->request->action(), $this->noLogin)) {
            return false;
        }
        return true;
    }
    // 检查过期时间
    public function checkTime()
    {
        if (Session::get('user_auth.exp_time') < date('Y-m-d',time())) {
            return false;
        }
        return true;
    }

    // 权限认证
    public function checkAuth()
    {
        if (Session::get('user_auth.user_id') != '1' &&
            !in_array($this->request->action(), $this->noLogin) &&
            !in_array($this->request->action(), $this->noAuth) &&
            !(new \core\UserAuth())->check('user/'
                . to_under_score($this->request->controller()) . '/'
                . $this->request->action(), Session::get('user_auth.user_id'))) {
            return false;
        }
        return true;
    }

    
}
